let userId = localStorage.getItem("employeeId");
let x;
for (let i = 0; i < AllStaffList.length; i++) {
    if (userId == AllStaffList[i].Id) {
        x = i;
    }
}


/*初始化界面*/
$(document).ready(function () {
    $(".touxiang").attr("src", `${AllStaffList[x].Url}`);
    $(".name1").html(AllStaffList[x].Name);
    $(".name").html(AllStaffList[x].Name);
    $(".sex").html(AllStaffList[x].Sex);
    $(".age").html(AllStaffList[x].Age);
    $(".quanxian").html(AllStaffList[x].DepName);
    $(".duty").html(AllStaffList[x].Duty);
    $(".email").html(AllStaffList[x].Email);
    $(".tel").html(AllStaffList[x].Tel);
    $(".address").html(AllStaffList[x].Addr);

    $(".gaiEmail").val($(".email").text());
    $(".gaiTel").val($(".tel").text());
    $(".gaiAddress").val($(".address").text());

    /*展示行程*/
    for (let i = 0; i < plans.length; i++) {
        let str = `<div class="plans"><button type="button" class="close plansClose" aria-label="Close" onclick="clos(this)"><span aria-hidden="true">&times;</span></button><p class="plans-time">${plans[i].planTime}</p><p class="plans-tit">${plans[i].planTit}</p><div class="plans-thing">${plans[i].planThing}</div></div>`;
        $(".plan").append(str);
    }
})

/*修改邮箱、电话、地址*/
$("#saveNew").click(function () {
    $(".telWrong").text("");
    $(".emailWrong").text("");
    /*验证手机号码格式*/
    if (!$(".gaiTel").val().match(/^1[34578]\d{9}$/)) {
        if ($(".telWrong").text() == "") {
            $(".telWrong").text("电话格式错误");
        } else {
            $(".telWrong").text("");
        }
    }
    /*验证邮箱格式*/
    if (!$(".gaiEmail").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
        if ($(".emailWrong").text() == "") {
            $(".emailWrong").text("邮箱格式错误");
        } else {
            $(".emailWrong").text("");
        }
    }
    if ($(".gaiTel").val().match(/^1[34578]\d{9}$/) && $(".gaiEmail").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
        /*手动隐藏模态框*/
        $('#myModal').modal('toggle');
        $('#myModal').on('hidden.bs.modal', function (e) {
            AllStaffList[x].ETel = $(".gaiTel").val();
            AllStaffList[x].Email = $(".gaiEmail").val();
            AllStaffList[x].Addr = $(".gaiAddress").val();
            $(".tel").html(AllStaffList[x].ETel);
            $(".email").html(AllStaffList[x].Email);
            $(".address").html(AllStaffList[x].Addr);
        })
        if ($(".telWrong").text() != "") {
            $(".telWrong").text("");
        }
        if ($(".emailWrong").text() != "") {
            $(".emailWrong").text("");
        }
    }
})
$(".newPassword").click(function () {
    /*验证密码格式*/
    if ($(".gaiPassword").val().match(/^[0-9A-Za-z]{6,12}$/)) {
        if ($(".gaiPassword").val() == $(".gaiPasswordAgain").val() && $(".gaiPassword").val() != AllStaffList[x].PassWord) {
            AllStaffList[x].PassWord = $(".gaiPassword").val();
            alert("修改成功！");
        } else if ($(".gaiPassword").val() == $(".gaiPasswordAgain").val() && $(".gaiPassword").val() == AllStaffList[x].PassWord) {
            alert("密码不能与原密码相同，请重新输入。");
        } else if ($(".gaiPassword").val() != $(".gaiPasswordAgain").val()) {
            alert("两次输入的密码不一致，请重新输入。");
        }
    } else {
        alert("密码格式错误。");
    }
    $(".gaiPassword").val("");
    $(".gaiPasswordAgain").val("");
})

/*添加行程*/
$(".addPlanBtn").click(function () {
    if ($("#eventTime").val() != "" && $("#eventTit").val() != "" && $("#eventThing").val() != "") {
        $(".plan").append(`<div class="plans"><button type="button" class="close plansClose" aria-label="Close" onclick="clos(this)"><span aria-hidden="true">&times;</span></button><p class="plans-time">${$("#eventTime").val()}</p><p class="plans-tit">${$("#eventTit").val()}</p><div class="plans-thing">${$("#eventThing").val()}</div></div>`)
        let obj = {
            id: plans.length + 1,
            planTime: $("#eventTime").val(),
            planTit: $("#eventTit").val(),
            planThing: $("#eventThing").val()
        }
        plans.push(obj);

    } else {
        alert("事件时间、标题和内容都不可为空");
    }
    $("#eventTime").val("");
    $("#eventTit").val("");
    $("#eventThing").val("");
    $(".noPlan")[0].outerHTML = "";

})

function clos(num) {
    console.log($(num).parent());
    for (let i = 0; i < plans.length; i++) {
        if ($(num).parent().get(0) == $(".plans")[i]) {
            $(num).parent().remove();
            plans.splice(i, 1);
            console.log("aaa");
        }
    }

    if (plans.length == 0) {
        $(".plan")[0].innerHTML = `<div class="noPlan">没有行程</div>`;
    }


}

